<template>
    <div class="cardRw_container">
        <el-form :model="ruleForm" label-width="100px" class="cardRw">
            <el-form-item label="设备号:" prop="device">
                <el-input v-model="ruleForm.device"></el-input>
            </el-form-item>
            <el-form-item label="UID:" prop="uid">
                <el-input v-model="ruleForm.uid"></el-input>
                <div class="cardRw_container_btn">
                    <el-button type="primary" @click="handleGetUid">读UID(卡号)</el-button>
                    <el-button type="primary" @click="handleOpenDevice">打开设备</el-button>
                </div>
            </el-form-item>
            <el-form-item label="块号:" prop="blockNumber">
                <el-input v-model="ruleForm.blockNumber"></el-input>&nbsp;&nbsp;0~N
            </el-form-item>
            <el-form-item label="秘钥:" prop="scretKey" width="120">
                <div class="cardRw_container_flex"><el-input v-model="ruleForm.scretKey"></el-input>12位(6字节),秘钥A</div>
            </el-form-item>
            <el-form-item label="数据:" prop="data">
                <el-input v-model="ruleForm.data"></el-input>32位(16字节)
                <div class="cardRw_container_btn">
                    <el-button type="primary" @click="handleReadIcCard">读卡</el-button>
                    <el-button type="primary" @click="handleWriteIcCard">写卡</el-button>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import ReaderFunction from './util/ReaderFunction'
import RFIDReader from './util/RFIDReader'
export default {
    name: 'cardRW',
    data() {
        return {
            ruleForm: {
                device: '0',
                blockNumber: "0",
                uid: '',
                scretKey: "FFFFFFFFFFFF",
                data: '',
                result: ''
            },
        }
    },
    created() {

    },
    mounted() {
    },
    methods: {
        handleGetUid() {
            let _this = this;
            ReaderFunction.TyA_GetUID(parseInt(this.ruleForm.device), function (result) {
                if (result !== null) {
                    RFIDReader.Sys_SetBuzzer(RFIDReader.device_id, 20, null);
                    const { snr } = result.data;
                    _this.ruleForm.uid = snr;
                }
            })
        },
        handleOpenDevice() {
            let _this = this;
            ReaderFunction.OpenDevice(this.ruleForm.device, function (result) {
                const { device_id } = result.data;
                _this.ruleForm.device = device_id;
            });
        },
        handleReadIcCard() {
            if (this.ruleForm.blockNumber.length == 0) {
                this.$message.error("请输入块号")
            }
            let _this = this;
            ReaderFunction.TyA_CS_Read2(parseInt(this.ruleForm.device), this.ruleForm.blockNumber, this.ruleForm.scretKey, function (result) {
                if (result !== null) {
                    RFIDReader.Sys_SetBuzzer(RFIDReader.device_id, 20, null);
                    const { value } = result.data;
                    _this.ruleForm.data = value;
                }
            })
        },
        handleWriteIcCard() {
            if (!(this.ruleForm.blockNumber.length !== 0 && this.ruleForm.data !== 0)) {
                this.$message.error("请输入块号,读卡之后在写卡")
            }
            ReaderFunction.TyA_CS_Write2(parseInt(this.ruleForm.device), this.ruleForm.blockNumber, this.ruleForm.scretKey, this.ruleForm.data, function (result) {
                if (result !== null) {
                    RFIDReader.Sys_SetBuzzer(RFIDReader.device_id, 20, null);
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.cardRw_container {
    width: 800px;
    margin: 0 auto;
    padding-top: 15px;

    &_flex {
        display: flex;
    }

    &_btn {
        display: flex;
        margin-top: 15px;
    }
}

::v-deep .el-input {
    width: 70%;
    margin-right: 10px;
}
</style>